package com.webcamtracker.tracker.model;

import com.webcamtracker.image.grid.ImageGrid;


public abstract class AbstractTrackerModel implements TrackerModel {
    private String name;


    public int getMaxX() {
        return getX() + getWidth();
    }

    public int getMaxY() {
        return getY() + getHeight();
    }

    public int getCenterX() {
        return getX() + getWidth() / 2;
    }

    public int getCenterY() {
        return (getY() + getHeight() / 2);
    }


    public double getWidthInCell(ImageGrid grid) {
        return this.getWidth() / grid.getCellHeight();
    }

    public double getHeightInCell(ImageGrid grid) {
        return this.getHeight() / grid.getCellHeight();
    }

    public int getMaxCellY(ImageGrid grid) {
        return this.getMaxY() / grid.getCellWidth();
    }

    public int getMinCellX(ImageGrid grid) {
        return this.getX() / grid.getCellWidth();
    }

    public int getMinCellY(ImageGrid grid) {
        return this.getY() / grid.getCellHeight();
    }

    public int getMaxCellX(ImageGrid grid) {
        return this.getMaxX() / grid.getCellHeight();
    }

    public float getZoomRatio(TrackerModel model) {

        int targetWidth = model.getWidth();
        int targetHeight = model.getHeight();

        double targetRatio = (double) targetWidth / targetHeight;

        int widthVariation = targetWidth - getWidth();
        int heightVariation = targetHeight - getHeight();

        int modelWidth;

        if (Math.abs(widthVariation) <= Math.abs(heightVariation)) {
            modelWidth = getWidth();
        } else {
            modelWidth = (int) (getHeight() * targetRatio);
        }

        return ((float) modelWidth / targetWidth);
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
